/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-07-23
 * Time: 16:57
 */
class MyBlockingQueue {
    public int size = 0;
    public int head = 0;
    public int tail = 0;
    public String[] str = null;

    public MyBlockingQueue(int capacity) {
        str = new String[capacity];
    }

    public void put(String s) throws InterruptedException {
        synchronized (this) {
            if (size >= this.str.length) {
                this.wait();//超出容量
            }
            this.str[tail] = s;
            tail++;
            if (tail >= this.str.length) {
                tail = 0;
            }
            size++;
            this.notify();
        }
    }

    public String take() throws InterruptedException {
        synchronized(this) {
            if(size == 0) {
                this.wait();
            }
            String str1 = str[head];
            head++;
            if(head >= str.length) {
                head = 0;
            }
            size--;
            this.notify();
            return str1;
        }
    }
}

public class Demo2 {

}
